#-*-coding:utf-8-*-

import re
import time
import json
import pymysql
import redis
import base64
import requests
import hashlib
from pyquery import PyQuery as pq
# from ..settings import REDIS_SAVEKEYS
from stuff.stuff.settings import *
# from .ip_api import IpList
from urllib import parse
from .mysql_connect import getPTConnection

class Untiles(object):
    COUNT = 0
    @classmethod
    def check_str(cls,string):
        """清洗字符串"""
        pattern = re.compile('【|】| |\n|\r|\t|\xa0|<.*?>|{.*?}|\u3000|(function.*?\})',re.S)
        string = re.sub(pattern,'',string).replace('(sinaads=window.sinaads||[]).push()','')
        if '收起公告重磅组团赚百万车币报名就拿奖' in string or '吉普牧马人换代，为销量放弃越野，国产车的机会来了' in string:
            string = cls().check_china(string)
        return string

    @classmethod
    def check_china(cls, string):
        res = ''.join(re.findall(re.compile('[\u4e00-\u9fa5]'), string))
        return res

    @classmethod
    def until_str(cls,string):
        pattern = re.compile('\n| |sm.r*?(.*?)\)', re.S)
        string = re.sub(pattern, '', string)
        return string

    @classmethod
    def check_style(cls,string):
        string = re.sub(re.compile('<style.*?</style>|<script.*?</script>',re.S),'',string)
        _str = pq(string)
        string   = _str.text()
        string = re.sub(re.compile(' |\n',re.S),'',string)
        return string

    @classmethod
    def get_url(cls,url):
        """从定向获取url"""
        if 'so.com' in url or 'baidu.com' in url or 'sogou.com' in url:
            if '//m.' in url:
                User = 'Mozilla/5.0 (Linux; Android 4.4.2; OPPO R11 Build/NMF26X) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36'
            else:
                User = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
            headers = {
                "User-Agent":'{}'.format(User),
                "Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
                'Cache-Control': 'max-age=0',
                'Connection': 'keep-alive',
                "Upgrade-Insecure-Requests": "1"
            }
            try:
                response = requests.get(url=url,allow_redirects=False,headers=headers,timeout=5)
            except Exception as e:
                if cls.COUNT > 5:
                    cls.COUNT=0
                    return url

                else:
                    time.sleep(.5)
                    cls.COUNT += 1
                    print('重定向url错误次数{}'.format(cls().COUNT))
                    url = cls.get_url(url)
                    return url
            else:
                if response.status_code == 302:
                    try:
                        if not "http" in response.headers['location']:
                            return url
                        return response.headers['location']  # 返回指向的地址
                    except:
                        pass
                else:
                    try :
                        if 'url' in response.text:
                            url = re.search(re.compile('url=(.*?)\"'),response.text).group(1)
                        else:
                            url = re.search(re.compile("URL=(.*?)>"), response.text).group(1).replace("'", '').replace('"', '')
                    except Exception as e:
                    # with open('get_url.html','w',encoding='utf-8') as f:
                    #     f.write(response.text)
                        print(e)
                        return url
        return url


def now_to_timestamp(digits = 10):
    """获取13位时间"""
    time_stamp = time.time()
    digits = 10 ** (digits -10)
    time_stamp = int(round(time_stamp*digits))
    return time_stamp

def sendstr(redis_key):
    _res = [eval(bytes.decode(x)) for x in redis_key]
    return _res

def get_comefrom(string,code):
    list1 = re.findall(re.compile('_(\w+)'),string)
    list2 = re.findall(re.compile('-(\w+)'),string)
    list3 = re.findall(re.compile('- (\w+)'),string)
    list4 = re.findall(re.compile('- (\w+)'),string)
    list5 = []
    if '|' in string:
        list5 = re.findall(re.compile('|(\w+)'),string)
        list5 = string.replace('|',',').split(',')
        print(list5)
    if list1:
        """配置"""
        if '_' in ''.join(list1):
            list1 = ''.join(list1).split('_')
            if not list1[-1]:
                list1 = list1[:-1]
        _list = list1[-1]
        if '配置' in ''.join(list1):
            _list = code
    if list2:
        if  '贴吧' in ''.join(list2):
            _list = '百度贴吧'
        elif '玩车' in ''.join(list2):
            _list = code
        else:
            _list = list2[-1]
    if  '优酷' in string:
        _list = '优酷网'
    if list3:
        if '【' in ''.join(list3[-1]):
            _list = ''.join(list3[-1]).replace('【','').replace('】','')
        else:
            _list = list3[-1]
    if list4:
        _list = list4[-1]
    if list5 != []:
        _list = list5[-1]
    if not list4 and  not list1 and not list2:
        _list = code
    return _list


class CheckMysql(object):

    def __init__(self):
        self.time = datetime.datetime.now().strftime("%Y%m%d")

    def select(self,url):
        with getPTConnection() as db:
            ls = ""
            sql = 'select url from urls'
            db.cursor.execute(sql)
            res = db.cursor.fetchall()
            for x in [z.decode('utf-8') for x in res for z in x]:
                if x in url:
                    print('url{}在过滤器中'.format(url))
                    ls = x
            if ls:
                return True
            else:
                return False

    def get_key(self):
        with getPTConnection() as db:
            sql = 'select word,project_id from warn_key_models where {} >= start AND {} <= over'.format(self.time,self.time)
            db.cursor.execute(sql)
            res = db.cursor.fetchall()
            res = [(x[0].decode('utf-8'),x[1]) for x in res]
            return res

    def return_sorcur(self):
        with getPTConnection() as db:
            return db.cursor

class RedisCheck(object):
    def __init__(self):
        if REDIS_PASSWD:
            pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,password=REDIS_PASSWD)
        else:
            pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT)
        self._r = redis.StrictRedis(connection_pool=pool)

    def redis_connect(self,db):
        self._r.connection_pool.connection_kwargs['db'] = db
        return self._r
def get_pull(key,url):
    start_url = 'https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word={}'.format(key)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
        "Referer": '{}'.format(url)}
    response = requests.get(url=start_url, allow_redirects=False, headers=headers,timeout=5)
    res = response.content.decode('utf-8')
    res = re.search(re.compile('suggest_so\((.*?)\)'),res).group(1)
    json_res = json.loads(res)

def now_to_timestamp(digits = 10):
    """获取13位时间"""
    time_stamp = time.time()
    digits = 10 ** (digits -10)
    time_stamp = int(round(time_stamp*digits))
    return time_stamp


def getMD5(item):
    # """获取MD5加密后的数据"""
    item_dict = dict(item)
    time = datetime.datetime.now().strftime('%Y%m%d')
    item_dict['time'] = time
    MD5_item_ = json.dumps(item_dict)
    # bytesString = res.encode(encoding="utf-8")
    # MD5_item_ = base64.b64encode(bytesString)
    """解码"""
    # decodestr = base64.b64decode(encodestr)
    return MD5_item_

